package com.szml.logging.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.szml.logging.dto.LogEvent;
import com.szml.logging.entity.OperationLog;
import com.szml.logging.service.LoggingService;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.annotation.SelectorType;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * @Author limin
 * @date 2025-06-21 15:24
 * @description
 */
@Component
@RocketMQMessageListener(topic = "REGISTER",
        consumerGroup = "log-service-consumer-group",
        selectorType = SelectorType.TAG) ////tag过滤模式
public class AsyncConsumerListener implements RocketMQListener<MessageExt> {
    @Autowired
    private LoggingService loggingService;

    @Override
    public void onMessage(MessageExt message) {
        CompletableFuture.runAsync(()->{
            try {
                processMessage(message);
            }catch (Exception e){
                e.printStackTrace();
            }
        });
    }
    private void processMessage(MessageExt message) {
        try {
            // 解析消息内容
            LogEvent event = JSON.parseObject(new String(message.getBody()), LogEvent.class);
            //根据tag来过滤，选择插入到不同的表
            String tag = message.getTags();
            OperationLog log = convertToOperationLog(event);
            int ret = 0;
            switch (tag) {
                case "register":
                    ret = loggingService.saveRegisterLog(log);
                    break;
                case "login":
                    ret = loggingService.saveLoginLog(log);
                    break;
                case "update":
                    ret = loggingService.saveUpdateLog(log);
                    break;
                default:
                    throw new RuntimeException("未知的tag");
            }
            if (ret != 1) {
                throw new RuntimeException("日志信息插入失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private OperationLog convertToOperationLog(LogEvent event) {
        OperationLog log = new OperationLog();
        log.setUserId(event.getUserId());
        log.setAction(event.getAction());
        log.setIp(event.getIp());
        log.setDetail(event.getDetail());
        return log;
    }


}
